package com.emily.mall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emily.common.utils.PageUtils;
import com.emily.common.utils.Query;

import com.emily.mall.product.dao.CategoryDao;
import com.emily.mall.product.entity.CategoryEntity;
import com.emily.mall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询所有的商品类别，并将其转化为树形结构
     * @param params  参数
     * @return 返回值
     */
    @Override
    public List<CategoryEntity> queryPageWithTree(Map<String, Object> params) {
        //1.获取所有的商品分类信息
        List<CategoryEntity> categoryList = baseMapper.selectList(null);
        //2.将商品分类信息拆解为树形结构 从集合中遍历出所有的父节点  即parent_cid=0的数据列表
        List<CategoryEntity> list = categoryList.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map(categoryEntity -> {
                    categoryEntity.setChildrens(getCategoryChildrens(categoryEntity,categoryList));
                    return categoryEntity;
                }).sorted((entity1, entity2) -> {
                    return (entity1.getSort() == null ? 0 : entity1.getSort()) - (entity2.getSort() == null ? 0 : entity2.getSort());
                }).collect(Collectors.toList());
        return list;
    }

    /**
     * 实现三级分类的逻辑删除
     * @param catId 需要删除的分类集合
     * @return 执行结果
     */
    @Override
    public boolean removeByIds(List<Long> catId) {
        //判断需要删除的分类
        baseMapper.deleteBatchIds(catId);
        return false;
    }

    private List<CategoryEntity> getCategoryChildrens(CategoryEntity categoryEntity, List<CategoryEntity> categoryList) {
        List<CategoryEntity> list = categoryList.stream()
                                    .filter(entity -> {
                                            return entity.getParentCid().equals(categoryEntity.getCatId());
                                    }).map(entity -> {//递归遍历获取子节点下的子节点
                                            entity.setChildrens(getCategoryChildrens(entity, categoryList));
                                            return entity;
                                    }).sorted((entity1, entity2) -> {
                                        return (entity1.getSort() == null ? 0 : entity1.getSort()) - (entity2.getSort() == null ? 0 : entity2.getSort());
                                    }).collect(Collectors.toList());

        return list;
    }
}
